using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.Registry;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_Config_Registry_ui_FormPath : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxRegistry"; } }
	#endregion
	#region ViewState storage
	/// <summary>
	/// Stores the ID of the record being edited
	/// </summary>
	public int RecordID
	{
		get
		{
			object o = ViewState["RecordID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["RecordID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the parent key
	/// </summary>
	public int ParentID
	{
		get
		{
			object o = ViewState["PID"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["PID"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Store any state information from the calling listing
			this.ParentID = GetAttribute("ParentID", -1);
			this.RecordID = GetAttribute("ID", -1);

			// Load the record if specified
			if (this.RecordID != -1)
			{
				RegistryDataManager rdm = new RegistryDataManager();
				RegistryDataNode node = rdm.GetByID( this.RecordID );
				if( node == null ) GoBack();

				KeyName.Text = node.KeyName;
			}
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Returns to the listing, restoring state
	/// </summary>
	protected void GoBack()
	{
		string url = String.Format("report.aspx?RPath={0}", this.ParentID);
		Response.Redirect(url, true);
	}
	#endregion
	#region Save/Cancel handlers
	/// <summary>
	/// User cancelled the save, go back to previous page
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// User clicked the save button, store the data
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (!Page.IsValid) return;
		RegistryDataManager rdm = new RegistryDataManager();
		RegistryDataNode node = null;

		if( this.RecordID != -1 )
		{
			node = rdm.GetByID( this.RecordID );
			if( node == null ) GoBack();
		}
		else
		{
			node = new RegistryDataNode();
			node.FK_ParentID = this.ParentID;
			node.ValueString = "";
			node.KeyType = (int) FluxRegistryKeyType.NODEPATH;
		}

		node.KeyName = KeyName.Text;
		rdm.Save( node );

		// Get information for auditing
		string path = "";
		RegistryDataNode trav = node;
		while (trav != null)
		{
			path = String.Format("{0}/{1}", trav.KeyName, path);
			trav = trav.Parent;
		}
		
		QuickAudit.RaiseInformation(
			new AuditUser(),
			"Flux", "Registry Editor",
			"created/edited path {0}",
			path );
		
		GoBack();
	}
	#endregion
}
